package Amazon;

public class TreeAmpli {
	
	public class Tree{
		public int x;
		public Tree l;
		public Tree r;
	}
	
	public static int amp = Integer.MIN_VALUE;
	public int solution(Tree T){
		if(T==null)
			return 0;
		helper(T,Integer.MAX_VALUE,Integer.MIN_VALUE);
		return amp;
	}
	
	private void helper(Tree T, int min, int max){
		if(T.x<min)
			min = T.x;
		if(T.x>max)
			max = T.x;
		if(T.l==null && T.r==null)
			amp = Math.max(amp, max-min);
		
		if(T.l!=null){
			helper(T.l, min, max);
		}
		if(T.r!=null){
			helper(T.r, min, max);
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
